package com.hnkywl.sts.service.wlgl;

import com.hnkywl.sts.dao.wlgl.ZjglDao;
import com.hnkywl.sts.entity.organ.Ysgj;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-6-4
 * Time: 上午12:31
 * To change this template use File | Settings | File Templates.
 */
@Service
public class ZjglService {
    private ZjglDao zjglDao;

    @Transactional
    public String findZjglList(String wlsmc,Long wlsId,String ysgjlx ,Long ysgjlxId,int pageNo, int pageSize) {
        Page<Ysgj> zjglPage = this.zjglDao.findZjglList(wlsmc,wlsId,ysgjlx,ysgjlxId,pageNo, pageSize);
        String zjglJson = BaseUtils.fetchCollectionToJson(zjglPage.getResult(), "id", "ysgjlx.id", "ysgjlx.z", "ysgjlx.bz", "ysgjsbm", "gps", "xm", "sjhm", "wls.id", "wls.qc");
        return "{\"total\":" + zjglPage.getTotalCount() + ",\"rows\":[" + zjglJson + "]}";

    }

    //查询载具详细信息
    @Transactional
    public Ysgj findYsgjDetail(Long id) {
        return this.zjglDao.get(id);
    }

    // 保存载具
    @Transactional
    public void saveZjgl(Ysgj ysgj) throws RuntimeException {
        if (this.zjglDao.checkQc(ysgj.getId(), ysgj.getYsgjsbm())) {
            throw new RuntimeException("运输工具已存在！");
        }
        ysgj.setDateCreated(new Date());
        ysgj.setLastUpdated(ysgj.getDateCreated());
        this.zjglDao.save(ysgj);
    }

    //删除载具
    @Transactional
    public void deleteZjgl(Long id) {
        Ysgj ysgj = zjglDao.get(id);
        ysgj.setLastUpdated(new Date());
        ysgj.setDeleted(1);
        this.zjglDao.save(ysgj);
    }

    //修改载具
    @Transactional
    public void updateZjgl(Ysgj ysgj) {
       Ysgj clone = this.findYsgjDetail(ysgj.getId());
        clone = (Ysgj) ReflectionUtils.getInstance(clone, ysgj);
        clone.setLastUpdated(new Date());
        this.zjglDao.save(clone);
    }

    @Autowired
    public void setZjglDao(ZjglDao zjglDao) {
        this.zjglDao = zjglDao;
    }
}
